* Placebo tests with participation in other programs
* 5/3/2025

clear all
set more off

program main
	read_cps
	foreach design in sc state triple {
		sample_restrictions, design(`design')
	}
	foreach outcome in mcaid liheap {
		foreach design in sc state triple {
			twfe_regressions, outcome(`outcome') design(`design')
		}
		generate_tables, outcome(`outcome')
		
		dynamic_regressions, outcome(`outcome')
	}
end

program read_cps  // parallel restrictions from 01_read_ipums.do
	// CPS downloaded from IPUMS here: https://cps.ipums.org/cps-action/data_requests/download
	// downloaded May 25, 2025
	use "$raw/cps_00016.dta", clear
	gisid serial pernum year
	
	assert !mi(year) & inrange(year, 2000, 2016)
	assert !mi(asecwt)
	assert !mi(age)
	assert !mi(statefip)
	keep if age >= 60 // include age 60-65 for less restrictive CAPs
	
	* merge in SNAP policy adoption years
	merge m:1 statefip using "$for_analysis/snap_policies", assert(2 3) keep(3) nogen 
		
	* merge in state-level covariates
	decode statefip, gen(state)
	di "merge unemployment"
	merge m:1 state year using "$intermediate/state_unemp_0016", ///
		assert(2 3) keep(3) nogen
	di "merge political data"
	merge m:1 state using "$intermediate/state_political", ///
		assert(1 2 3) keep(1 3) gen(merge_political)
	di "merge median HH income"
	merge m:1 state using "$intermediate/state_income", ///
		assert(1 2 3) keep(1 3) gen (merge_med_inc)
	assert statefip == 11 if (merge_political == 1 | merge_med_inc == 1) // Washington D.C.
	drop merge_*
	ren (perc_democrat unemp) (perc_democrat_2004 state_unemp)
	
	* save dataset for state-level design (both treated and control states)
	save "$intermediate/states_cps", replace
	
	* restrict to treated states for singles-couples design
	keep if inlist(statefip, 45, 12, 25, 36, 48, 53, 28, 37) | ///
		inlist(statefip, 42, 51, 21, 22, 26, 46, 4, 34, 35, 24)
	tab statefip
	save "$intermediate/sc_cps", replace
end

program sample_restrictions  // parallel restrictions from 02_restrict_sample.do
	syntax, design(str)

	if "`design'" == "sc" {
		use "$intermediate/sc_cps", clear
		drop if marst == 2 // restrict to household type explicitly live-alone or couple
	}
	else if "`design'" == "state" {
		use "$intermediate/states_cps", clear
		* keep only live-alone HH types, or all individuals in states w/o a restriction to singles
		keep if (marst >= 3) | ///
			(statefip == 35 | statefip == 48 | statefip == 21 | statefip == 46) 
		drop if statefip == 35 & year > 2013 // New Mexico: only had the CAP for 5 years and then eliminated it
	}
	else if "`design'" == "triple" {
		use "$intermediate/states_cps", clear
		drop if marst == 2	// drop ambiguous household types
	}
	
	* state-specific eligibility criteria 
	drop if age < 65 & inlist(statefip, 37, 51, 34, 4) // these states require age 65+
	assert !(incwage == 99999999 | incwage == 99999998) // missing values
	keep if incwage == 0 | inlist(statefip, 36, 37, 22, 21, 48, 4, 46) // no earned income
	
	gen single = (marst >= 3)
	
	*create education categories; check other covariates
	gen edcat = 1 if educ <= 71
	replace edcat = 2 if educ == 73
	replace edcat = 3 if educ >= 81 & educ <= 92
	replace edcat = 4 if educ > 100
	lab def edlabel 1 "HS dropout" 2 "HS grad" 3 "some college" 4 "college grad"
	lab val edcat edlabel
	
	foreach policy in bbce call_any faceini facerec oapp no_fp { // other state SNAP policies
		gen has_`policy' = (year > `policy'_startyear)
	}

	* define the target sample of self-reported SSI recipients
	su incssi, d
	assert !mi(incssi) & incssi != 999999 // code for missing
	keep if incssi > 0
	
	* generate outcome and treatment variables
	assert inlist(himcaidly, 1, 2)
	gen mcaid = (himcaidly == 2) // indicator for Medicaid
	assert inlist(heatsub, 1, 2)
	gen liheap = (heatsub == 2) // indicator for receipt of LIHEAP funds
	
	ren cap_startyear startyear
	replace startyear = 1995 if statefip == 45	// South Carolina
	
	gen relyr = year - startyear
	replace relyr = -6 if relyr < -6  // bin large relative years
	replace relyr = 6 if relyr > 6

	if "`design'" == "sc" { // restrict to states with single eligibility requirement
		keep if statefip == 45 | statefip == 28 | statefip == 53 | statefip == 36 | ///
			statefip == 25 | statefip == 12 | statefip == 37 | statefip == 42 | ///
			statefip == 51 | statefip == 22 | statefip == 26 | statefip == 34 | statefip == 24
		
		gen treat = (single == 1) // treatment is single and live alone
		replace relyr = 0 if treat == 0
		gen post = (year > startyear & !mi(startyear))
		gen inter = treat*post
	}
	else if "`design'" == "state" {
		replace relyr = 0 if startyear == .
		gen inter = (year > startyear & !mi(startyear))
	}
	else if "`design'" == "triple" {
		drop if inlist(statefip, 48, 21, 4, 46, 35) // drop states with a CAP but no singles requirement
		
		gen treated = (startyear != .)	// treated state
		replace relyr = 0 if mi(startyear)
		gen post = (year > startyear & !mi(startyear))	// post treatment year
		gen inter = (treated == 1 & post == 1)	// interaction of post and treat
		gen triple = (inter == 1 & single == 1)	// triple interaction
	}
	
	egen relyr_pos = group(relyr)
	labmask relyr_pos, values(relyr)
	save "$for_analysis/`design'_cps_for_regressions", replace
end

program twfe_regressions  // from takeup_regressions.do
	syntax, outcome(str) design(str)
	
	use "$for_analysis/`design'_cps_for_regressions", clear
	local indiv_controls = "i.sex i.race i.edcat age"
	local state_snap_controls = "i.has_bbce i.has_call i.has_faceini i.has_facerec i.has_oapp i.has_no_fp"
	
	if "`design'" == "sc" {
		ren inter inter_sc
		lab var inter_sc "Single x Post"
		reghdfe `outcome' inter_sc [w = asecwt], a(i.marst i.year) vce(cl statefip) // no controls
		est sto sc1_`outcome'
		qui estadd local cont " ", replace
		qui estadd local unemp " ", replace
		qui estadd local hhtype "\checkmark", replace
		qui estadd local stateFE " ", replace
		qui estadd local yearFE "\checkmark", replace

		reghdfe `outcome' inter_sc `indiv_controls' [w = asecwt], ///
			a(i.marst i.year i.statefip) vce(cl statefip)	// controls
		est sto sc2_`outcome'
		qui estadd local cont "\checkmark", replace
		qui estadd local unemp " ", replace
		qui estadd local hhtype "\checkmark", replace
		qui estadd local stateFE "\checkmark", replace
		qui estadd local yearFE "\checkmark", replace

		reghdfe `outcome' inter_sc `indiv_controls' ///
			state_unemp perc_democrat_2004 hh_income_2003 `state_snap_controls' ///
			[w = asecwt], a(i.marst i.year) vce(cl statefip) // state controls instead of state FE
		est sto sc3_`outcome'
		qui estadd local cont "\checkmark", replace
		qui estadd local unemp "\checkmark", replace
		qui estadd local hhtype "\checkmark", replace
		qui estadd local stateFE " ", replace
		qui estadd local yearFE "\checkmark", replace
	}
	else if "`design'" == "state" {
		lab var inter "Treat x Post"
		reghdfe `outcome' inter [w = asecwt], a(i.statefip i.year) vce(cl statefip)	// no controls
		est sto state1_`outcome'
		qui estadd local cont " ", replace
		qui estadd local unemp " ", replace
		qui estadd local hhtype " ", replace
		qui estadd local stateFE "\checkmark", replace
		qui estadd local yearFE "\checkmark", replace

		reghdfe `outcome' inter `indiv_controls' [w = asecwt], ///
			a(i.statefip i.year) vce(cl statefip)	// controls
		est sto state2_`outcome'
		qui estadd local cont "\checkmark", replace
		qui estadd local unemp " ", replace
		qui estadd local hhtype " ", replace
		qui estadd local stateFE "\checkmark", replace
		qui estadd local yearFE "\checkmark", replace

		reghdfe `outcome' inter `indiv_controls' ///
			state_unemp `state_snap_controls' ///
			[w = asecwt], a(i.statefip i.year) vce(cl statefip) // add state controls
		est sto state3_`outcome'
		qui estadd local cont "\checkmark", replace
		qui estadd local unemp "\checkmark", replace
		qui estadd local hhtype " ", replace
		qui estadd local stateFE "\checkmark", replace
		qui estadd local yearFE "\checkmark", replace
	}
	else if "`design'" == "triple" {
		lab var triple "Single x Treated State x Post"
		
		reghdfe `outcome' triple [w = asecwt], ///
			a(i.marst##i.year i.year##i.statefip i.statefip##i.marst) vce(cl statefip)	// no controls, only FEs
		est sto triple1_`outcome'
		qui estadd local cont " ", replace
		qui estadd local unemp " ", replace
		qui estadd local hhtype "\checkmark", replace
		qui estadd local stateFE "\checkmark", replace
		qui estadd local yearFE "\checkmark", replace

		// note: state SNAP controls are collinear with i.year##i.statefip for triple diff, so need to omit
		reghdfe `outcome' triple `indiv_controls' [w = asecwt], ///
			a(i.marst##i.year i.year##i.statefip i.statefip##i.marst) vce(cl statefip)  // controls and FEs
		est sto triple2_`outcome'
		qui estadd local cont "\checkmark", replace
		qui estadd local unemp " ", replace
		qui estadd local hhtype "\checkmark", replace
		qui estadd local stateFE "\checkmark", replace
		qui estadd local yearFE "\checkmark", replace
	}
end

program generate_tables 
	syntax, outcome(str)
	
	di "generate table for `outcome'"
	esttab state1_`outcome' state2_`outcome' state3_`outcome' ///
		sc1_`outcome' sc2_`outcome' sc3_`outcome' triple1_`outcome' triple2_`outcome', ///
		keep(inter inter_sc triple) order(inter inter_sc triple) ///
		varlabels(inter "Treated x Post" inter_sc "Single x Post" triple "Single x Treated x Post") ///
		se(%5.3f) star(* 0.10 ** 0.05 *** 0.01) stats(yearFE stateFE hhtype cont unemp N, ///
		labels("Year FE" "State FE" "Household FE" "Individual Controls" "State Controls" "N" ) ///
		fmt("%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc")) scalars() ///
		nomtitles label nonote booktabs
		
	esttab state1_`outcome' state2_`outcome' state3_`outcome' ///
		sc1_`outcome' sc2_`outcome' sc3_`outcome' triple1_`outcome' triple2_`outcome' ///
		using "$output/placebo_`outcome'", replace ///
		keep(inter inter_sc triple) order(inter inter_sc triple) ///
		varlabels(inter "Treated x Post" inter_sc "Single x Post" triple "Single x Treated x Post") ///
		se(%5.3f) star(* 0.10 ** 0.05 *** 0.01) stats(yearFE stateFE hhtype cont unemp N, ///
		labels("Year FE" "State FE" "Household FE" "Individual Controls" "State Controls" "N" ) ///
		fmt("%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc")) scalars() ///
		nomtitles label nonote booktabs
end 

program dynamic_regressions
	syntax, outcome(str)
	
	local indiv_controls = "i.sex i.race i.edcat age"
	local state_snap_controls = "i.has_bbce i.has_call i.has_faceini i.has_facerec i.has_oapp i.has_no_fp"
	local omit = 6
	
	use "$for_analysis/sc_cps_for_regressions", clear
	reghdfe `outcome' ib`omit'.relyr_pos `indiv_controls' ///
		[w = asecwt], a(i.marst i.year i.statefip) vce(cl statefip)
	est sto sc

	use "$for_analysis/state_cps_for_regressions", clear		
	reghdfe `outcome' ib`omit'.relyr_pos `indiv_controls' ///
		[w = asecwt], a(i.statefip i.year) vce(cl statefip)
	est sto state
			
	coefplot (sc, offset(-0.06) keep(*.relyr_pos) drop(1.relyr_pos 13.relyr_pos) ///
		ciopts(recast(rcap) lcolor(purple)) mcolor(purple) ///
		mfcolor(white) msymbol(T) msize(medsmall) label(Singles-Couples Design) baselevel) ///
		(state, offset(0.06) keep(*.relyr_pos) drop(1.relyr_pos 13.relyr_pos) ///
		ciopts(recast(rcap) lcolor(midgreen)) label(State-level Design) mcolor(midgreen) ///
		mfcolor(white) msymbol(D) msize(medsmall)), ///
		xtitle("Years Since CAP Adopted") ytitle("Average Effect and 95% CI") vertical ///
		xscale(titlegap(2)) xline(6.5, lp(dash) lcolor(gs12)) yline(0, lwidth(thin) ///
		lp(dash) lcolor(black)) ylab(#15, angle(0)) graphregion(fcolor(white) lcolor(white) ///
		lwidth(vvvthin)) legend(region(lstyle(none))) ///
		coeflabel(Tm5 = "-5" Tm4 = "-4" Tm3 = "-3" Tm2 = "-2" Tm1 = "-1" Tp0 = "0" ///
		Tp1 = "1" Tp2 = "2" Tp3 = "3" Tp4 = "4" Tp5 = "5") omitted baselevel
	graph export "$output/dynamic_`outcome'.png", replace
end


* Execute
main
